﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.ComponentModel;
using System.Reflection;
using System.Windows.Forms;
using ToolHelper.DataGridViewExtern;

namespace CommonLibrary.CustControls
{
    public enum BoolEnum
    {
        True,
        False
    };

    class CusToolboxItem
    {
        private String controlName = null;
        private String controlTitle = null;
        private String controlTypeName = null;
        private Type controlType = null;
        private Image controlImg = null;
        public static List<String> ControlNames = new List<string>();

        private BoolEnum fillable = BoolEnum.True;

        [DisplayName("可填写")]
        public BoolEnum Fillable
        {
            get { return fillable; }
            set { fillable = value; }
        }

        public CusToolboxItem(String controlName, String controlTitle, String controlTypeName)
        {
            this.controlName = controlName;
            this.controlTitle = controlTitle;
            this.controlTypeName = controlTypeName;
        }

        public CusToolboxItem(String controlName, String controlTitle, String controlTypeName, Boolean fillable)
        {
            this.fillable = fillable == true ? BoolEnum.True : BoolEnum.False;
            this.controlName = controlName;
            this.controlTitle = controlTitle;
            this.controlTypeName = controlTypeName;
        }

        [DisplayName("新建列类型")]
        public Type ControlType
        {
            get
            {
                if (controlType == null)
                    controlType = Type.GetType(controlTypeName);
                if (controlType == null)
                {
                    Assembly assembly = Assembly.GetAssembly(typeof(DataGridViewColumn));
                    controlType = assembly.GetType(controlTypeName);
                }
                return controlType;
            }
        }

        [Browsable(false)]
        public Image ControlImg
        {
            get
            {
                if (controlImg == null)
                {
                    Type t = ControlType;
                    if (t == null)
                        t = typeof(Component);
                    ToolboxBitmapAttribute attr = (ToolboxBitmapAttribute)
                        TypeDescriptor.GetAttributes(t)[typeof(ToolboxBitmapAttribute)];
                    if (attr != null)
                        controlImg = attr.GetImage(t, false);
                }
                return controlImg;
            }
        }

        [DisplayName("列名")]
        public String ControlName
        {
            get
            {
                return controlName;
            }
            set
            {
                if (ValidateColName(value))
                    controlName = value;
                else
                    MessageBox.Show("其他列已经在使用名称 " + value, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        [DisplayName("页眉文字")]
        public String ControlTitle
        {
            get
            {
                return controlTitle;
            }
            set
            {
                controlTitle = value;
            }
        }

        /// <summary>
        ///  列名验证
        /// </summary>
        /// <param name="colName"></param>
        /// <returns></returns>
        private Boolean ValidateColName(String colName)
        {
            foreach (var item in ControlNames)
            {
                if (item == colName)
                    return false;
            }
            return true;
        }
    }


    public class CopyGrdViewData
    {
        private static void AddCols(ref DataGridView newGrdView, DataGridView oriGrdView)
        {
            var columns = oriGrdView.Columns;
            if (columns == null)
                return;
            List<DataGridViewColumn> LstGrdCol = GetColList(oriGrdView.Columns);
            newGrdView.Columns.AddRange(LstGrdCol.ToArray());
        }

        public static void AddCols(ref DataGridViewColumnCollection newColumns, DataGridView oriGrdView)
        {
            var columns = oriGrdView.Columns;
            if (columns == null)
                return;
            List<DataGridViewColumn> LstGrdCol = GetColList(oriGrdView.Columns);
            newColumns.AddRange(LstGrdCol.ToArray());
        }

        private static List<DataGridViewColumn> GetColList(DataGridViewColumnCollection columns)
        {
            List<DataGridViewColumn> LstGrdCol = new List<DataGridViewColumn>();
            Int32 colIndex = 0;
            foreach (ExternPropTextBoxColumn col in columns)
            {
                var newCol = Activator.CreateInstance(typeof(ExternPropTextBoxColumn)) as ExternPropTextBoxColumn;
                if (newCol != null)
                {
                    newCol.DisplayIndex = colIndex;
                    newCol.HeaderText = col.HeaderText;
                    newCol.Name = col.Name;
                    newCol.Fillable = col.Fillable;
                    LstGrdCol.Add(newCol);
                    colIndex++;
                }
            }
            return LstGrdCol;
        }

        public static void InitData(ref DataGridView newGrdView, DataGridView oriGrdView)
        {
            newGrdView.Columns.Clear();
            AddCols(ref newGrdView, oriGrdView);

            var colCount = oriGrdView.Columns.Count;
            var rowCount = oriGrdView.Rows.Count;
            newGrdView.Rows.Clear();
            if (rowCount > 1)
                newGrdView.Rows.Add(rowCount - 1);
            var rows = oriGrdView.Rows;
            for (int rowIndex = 0; rowIndex < rowCount; rowIndex++)
                for (int colIndex = 0; colIndex < colCount; colIndex++)
                    newGrdView.Rows[rowIndex].Cells[colIndex].Value = oriGrdView.Rows[rowIndex].Cells[colIndex].Value;
        }
    }
}
